import {
  get,
  put,
  post,
  apiDelete,
  saveAccessToken,

} from './api';
import md5 from 'md5';
import {emit, subscribe} from '../utils/pubsub';

const PAGE_SIZE = 5;

let appList = [];

subscribe('/user/logout', () => {
  appList = [];
});

export function getList() {
  return appList;
}

export async function fetchList() {
  const { data } = await get(`/app/list`);
  appList = data;
  emit('/app/list/fetched');
}

export async function deleteApp(id) {
  await apiDelete(`/app/${id}`);
  appList = appList.filter(v=>v.id!==id);
}

export async function createApp(data) {
  const {id} = await post('/app/create', data);
  appList = [{id, ...data}].concat(appList);
}

const versionlimit = 10;
const packagelimit = 1000;

let versionList = [];

export function getVersionList(){
  return versionList;
}

export async function fetchVersion(appId, offset){
  var fetchURL = '/app/'+appId+'/version/list?offset=' + (offset?offset*10:0) + '&limit=' + versionlimit;
  var vl = await get(fetchURL);

  if (offset && offset>0){
    versionList = versionList.concat(vl.data);
  }else{
    versionList = vl.data;
  }
  emit('/app/version/fetched');
}

let packageListAll = [];
let packageListNoVersion = [];

export function getPackageList(type){
  if (type){
    if (type === "all"){
      return packageListAll;
    }else if (type === "noVersion"){
      return packageListNoVersion;
    }
  }
  return packageListAll;
}

export async function fetchPackage(appId, offset){
  var fetchURL = '/app/'+appId+'/package/list?offset='  + offset + '&limit=' + packagelimit;
  var pl = await get(fetchURL);
  packageListAll = pl.data;

  var fetchURL2 = '/app/'+appId+'/package/list?noVersion=1&offset='  + offset + '&limit=' + packagelimit;
  var pl2 = await get(fetchURL2);
  packageListNoVersion = pl2.data;

  emit('/app/package/fetched');
}


export async function putPackageIntoVersion(appId, packageId, VersionId) {
  var putURL = '/app/'+appId+'/package/'+packageId;
  var putData = {
    versionId: VersionId
  }

  var packageInfo = {};
  var needPut = true;
  //drop to itself, no move.
  packageListAll.map(p=>{
    if (p.version){
      if (p.id === parseInt(packageId) && p.version.id === parseInt(VersionId)){
        needPut = false;
      }
    }
    if (p.id === packageId){
      packageInfo = {
        id: p.id,
        name: p.name,
        status: p.status
      }
    }
  })

  if (!needPut){
    return;
  }

  await put(putURL, putData);

  versionList.map(v=>{
    //delete package from original version
    let opackageIdx = -1;
    for(let i=0;i<v.packages.length;i++){
      if (v.packages[i].id === packageId){
        opackageIdx = i;
        break;
      }
    }

    if (opackageIdx>=0){
      v.packages.splice(opackageIdx, 1);
    }

    //set package into new version
    if (v.id === VersionId){
      v.packages = v.packages.concat(packageInfo);
    }

  })

  await fetchPackage(appId, 0);

  emit('/app/package/put');
}

let packageWillDelete = false;

export function ifPackageWillDelete(){
  return packageWillDelete;
}

export function packageDragged(){
  packageWillDelete = true;
  emit('/app/package/willDelete');
}

export function packageDropeed(){
  packageWillDelete = false;
  emit('/app/package/willDelete');
}

export async function deletePackage(appId, packageId){
  var deleteURL = "/app/"+appId+"/package/"+packageId;

  await apiDelete(deleteURL);
  await fetchPackage(appId, 0);

  versionList.map(v=>{
    let opackageIdx = -1;
    for(let i=0;i<v.packages.length;i++){
      if (v.packages[i].id === packageId){
        opackageIdx = i;
        break;
      }
    }
    if (opackageIdx>=0){
      v.packages.splice(opackageIdx, 1);
    }
  })

  packageWillDelete = false;
  emit('/app/package/deleted');
}

export async function changeVersionInfo(appId, versionId, data){
  var putURL = '/app/'+appId+'/version/'+versionId;

  await put(putURL, data);
  await fetchVersion(appId, 0);
}

export async function deleteVersion(appId, versionId){
  var deleteURL = '/app/'+appId+'/version/'+versionId;

  await apiDelete(deleteURL);

  var pos = 0;
  for(var i=0;i<versionList.length;i++){
    if (versionList[i].id === parseInt(versionId)){
      pos = i;
      break;
    }
  }
  versionList.splice(pos, 1);
  emit('/app/version/deleted');
}

export async function getAppInfo(appId){
  var getURL = '/app/'+appId;

  var ret = await get(getURL);
  return ret;
}

export async function setAppInfo(appId, data){
  var putURL = '/app/'+appId;
  await put(putURL, data);

  for(var i=0;i<appList.length;i++){
    if (appList[i].id === parseInt(appId) && data.name){
      appList[i].name = data.name;
      break;
    }
  }
  emit('/app/info/changed');
}

export async function deleteApp(appId){
  var deleteURL = '/app/'+appId;

  await apiDelete(deleteURL);
}